var CometPrezRemote = {
	
    _connectionEstablished : function () {
    	Logger.info('CometD Connection Established');
    },

    _connectionBroken : function() {
    	Logger.error('CometD Connection Broken');
    },

    _connectionClosed : function() {
    	Logger.info('CometD Connection Closed');
    },
	
	_metaConnect : function(message) {
		var cometd = $.cometd;
		var settings = CometSettings;

		if (cometd.isDisconnected()) {
        	settings._connected = false;
        	CometPrezRemote._connectionClosed();
            return;
        }

        var wasConnected = settings._connected;
        settings._connected = message.successful === true;
        if (!wasConnected && settings._connected)
        {
        	CometPrezRemote._connectionEstablished();
        }
        else if (wasConnected && !settings._connected)
        {
        	CometPrezRemote._connectionBroken();
        }
    },
	
	_metaHandshake : function(handshake){
		var cometd = $.cometd;
		var settings = CometSettings;
		
        if (handshake.successful === true) {
            cometd.batch(function() {
            	cometd.publish('/service/prez/control',{
                	command:'remoting'
                });
            });
        }
    },
	
	init : function(){
		var cometd = $.cometd;
		var obj = this;

		// Disable the websocket transport
		cometd.websocketEnabled = false;
		
        cometd.addListener('/meta/handshake', obj._metaHandshake);
        cometd.addListener('/meta/connect', obj._metaConnect);

        cometd.handshake();
	},
	
	control : function(remoteAction) {
		var cometd = $.cometd;
		var settings = CometSettings;
        cometd.batch(function() {
            cometd.publish('/service/prez/control',{
            	command:'remotecontrol',
            	taskId:settings._taskId,
            	action:remoteAction
            });
        });
	}
};